package tech.moya.flexbase.ui
{
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.controls.treeClasses.ITreeDataDescriptor;
	
	import tech.moya.flexbase.beans.Connection;
	import tech.moya.flexbase.beans.Table;

	public class ConnectionTreeDescriptor implements ITreeDataDescriptor
	{
		public function ConnectionTreeDescriptor()
		{
		}

		public function getChildren(node:Object, model:Object=null):ICollectionView
		{
			
			if(node is Connection){
				return new ArrayCollection((Connection(node)).Tables);
			}else if(node is Table){
				return new ArrayCollection((Table(node)).Columns);
			}
			return new ArrayCollection();
		}
		
		public function hasChildren(node:Object, model:Object=null):Boolean
		{
			
			if(node is Connection){
				
				return ((Connection(node)).Tables!= null && (Connection(node)).Tables.length > 0);
			}else if(node is Table){
				return ((Table(node)).Columns!= null && (Table(node)).Columns.length > 0);
			}
			return false;
		}
		
		public function isBranch(node:Object, model:Object=null):Boolean
		{
			if(node is Connection){
				
				return ((Connection(node)).Tables!= null && (Connection(node)).Tables.length > 0);
			}else if(node is Table){
				return ((Table(node)).Columns!= null && (Table(node)).Columns.length > 0);
			}
			return false;
		}
		
		public function getData(node:Object, model:Object=null):Object
		{
			if(node is Connection){
				
				return (Connection(node)).Description;
			}
			return "No Data";
		}
		
		public function addChildAt(parent:Object, newChild:Object, index:int, model:Object=null):Boolean
		{
			return false;
		}
		
		public function removeChildAt(parent:Object, child:Object, index:int, model:Object=null):Boolean
		{
			return false;
		}
		
	}
}